package com.le.buddha.util;

/**
 * @description:
 * @author: guole
 * @date: 2020/4/11
 */
public class BinHexOctUtils {
    static final char[] digits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    public BinHexOctUtils() {
    }

    public static String hexOctString(long n, int base) {
        return hexOctString(n, base, digits);
    }

    public static String hexOctString(long n, int base, char[] digit) {
        String result = null;

        try {
            if (base > digit.length) {
                return result;
            }

            long num = 0L;
            if (n < 0L) {
                num = 4294967294L + n + 2L;
            } else {
                num = n;
            }

            char[] buf = new char[32];

            int charPos;
            for(charPos = 32; num / (long)base > 0L; num /= (long)base) {
                --charPos;
                buf[charPos] = digit[(int)(num % (long)base)];
            }

            --charPos;
            buf[charPos] = digit[(int)(num % (long)base)];
            result = new String(buf, charPos, 32 - charPos);
        } catch (Exception var9) {
        }

        return result;
    }

    public static long hexOctDecimal(String str, int base) {
        return hexOctDecimal(str, base, digits);
    }

    public static Long hexOctDecimal(String str, int base, char[] digit) {
        Long result = null;

        try {
            if (null == str || base > digit.length) {
                return result;
            }

            long num = 0L;
            char[] buf = new char[str.length()];
            str.getChars(0, str.length(), buf, 0);

            for(int i = 0; i < buf.length; ++i) {
                for(int j = 0; j < digit.length; ++j) {
                    if (digit[j] == buf[i]) {
                        num = (long)((double)num + (double)j * Math.pow((double)base, (double)(buf.length - i - 1)));
                        break;
                    }
                }
            }

            result = num;
        } catch (Exception var9) {
            result = null;
        }

        return result;
    }
}
